package com.timous.others;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode-cn.com/problems/isomorphic-strings/
 * @author Timous
 * @create 2020-12-27 17:52
 **/
public class _205_同构字符串 {
    public boolean isIsomorphic(String s, String t) {
        //两个map
        Map<Character, Character> s2t = new HashMap<Character, Character>();
        Map<Character, Character> t2s = new HashMap<Character, Character>();
        //判断s的长度
        int len = s.length();
        for (int i = 0; i < len; ++i) {
            //取出s和t对应的字符
            char x = s.charAt(i), y = t.charAt(i);
            //判断之前的这个字符是不是经过替换 ， 如果经过替换，看看是不是我们之前替换的字符
            if ((s2t.containsKey(x) && s2t.get(x) != y) || (t2s.containsKey(y) && t2s.get(y) != x)) {
                return false;
            }
            //如果不是，放入map ， 便于下次的判断
            s2t.put(x, y);
            t2s.put(y, x);
        }
        return true;
    }
}
